<?
$now_date=date("l, F j, Y \a\\t g:i A",strtotime("-2 hours")).", GMT";
//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='name, first_name, last_name, email';
	$sql['table']='resellers';
	$sql['where']="reseller_id='$id'";
	$sql['options']='';
	$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81)) {
	exit("<h4><font color=red>$database_error</font></h4>");
}
$fullname=$result[0][first_name]." ".$result[0][last_name];
$email[address]=$result[0][email];
$nick_name=$result[0][name];
$query="SELECT * FROM api_settings WHERE reseller_id='$id'";
if (!$result=pg_query($dbh81,$query)) {
	exit("<h4><font color=red>$database_error</font></h4>");
}
$num=pg_num_rows($result);
if ($num!=0) {
	$current_dmode=pg_fetch_result($result,0,'demo_mode');
	$current_addr=pg_fetch_result($result,0,'ip_addr');
	if (!empty($_POST[address]) && !ereg($_POST[address],$current_addr)) {
	        if (empty($current_addr)) {
                        //------------------------------------------------------
                                $sql['type']='UPDATE';
                                $sql['params']="ip_addr='$_POST[address]'";
                                $sql['table']='api_settings';
                                $sql['where']="reseller_id='$id'";
                                $sql['options']='';
                                $sql['return']='struct';
                        //------------------------------------------------------
                        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
                                exit("<h4><font color=red>$database_error</font></h4>");
                        }
                } else {
                        $addr=$current_addr.",$_POST[address]";
                        //------------------------------------------------------
                                $sql['type']='UPDATE';
                                $sql['params']="ip_addr='$addr'";
                                $sql['table']='api_settings';
                                $sql['where']="reseller_id='$id'";
                                $sql['options']='';
                                $sql['return']='struct';
                        //------------------------------------------------------
                        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
                                exit("<h4><font color=red>$database_error</font></h4>");
                        }
                }
                //------------------------------------------------------
                        $sql['type']='SELECT';
                        $sql['params']='*';
                        $sql['table']='api_settings';
                        $sql['where']="reseller_id='$id'";
                        $sql['options']='';
                        $sql['return']='struct';
                //------------------------------------------------------
                if (!$result=sql_exec($sql,$dbh81,'assoc')) {
                        exit("<h4><font color=red>$database_error</font></h4>");
                }
                $addr=$result[0][ip_addr];
                if (empty($addr)) {
                        $addr='none';
                } else {
                        $addr=preg_replace("/,/","\n",$addr);
                }

                $SETTINGS["baseTMName"]="ResselersPanel Team";
                $SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

                $mail_array= getMail ("api_ip_added", Array(
                                "fullname"=>$fullname,
                                "ip"=>$_POST[address],
                                "ip_list"=>$addr,
                                "username"=>$nick_name,
                                "date"=>$now_date,
                                "mainserver" => "ResellersPanel"
                                ), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                $email[from]= $mail_array[from];
                $email[mime]= $mail_array[mime];
                $email[subject]= $mail_array[subject];
                $email[body]=$mail_array[body];
                if (!mail($email[address],$email[subject],$email[body],
                        "From: ".$email[from]."\r\n"
                        ."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
                                $mail=serialize($email);
                                mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
                }
	}
	if ($_POST[radio]!="$current_dmode") {
		//------------------------------------------------------
			$sql['type']='UPDATE';
			$sql['params']="demo_mode='$_POST[radio]'";
			$sql['table']='api_settings';
			$sql['where']="reseller_id='$id'";
			$sql['options']='';
			$sql['return']='struct';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81,'assoc')) {
			exit("<h4><font color=red>$database_error</font></h4>");
		}
		if ($_POST[radio]=='f') {
			$demo='off';
		} elseif ($_POST[radio]=='t') {
			$demo='on';
		}
		$SETTINGS["baseTMName"]="ResselersPanel Team";
		$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

		$mail_array= getMail ("api_demo_changed", Array(
				"fullname"=>$fullname,
				"demo"=>$demo,
				"username"=>$nick_name,
				"date"=>$now_date,
				"mainserver" => "ResellersPanel"
				), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

		$email[from]= $mail_array[from];
		$email[mime]= $mail_array[mime];
		$email[subject]= $mail_array[subject];
		$email[body]=$mail_array[body];
		if (!mail($email[address],$email[subject],$email[body],
			"From: ".$email[from]."\r\n"
			."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
				$mail=serialize($email);
				mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
		}
	}
} else {
	if (empty($_POST[radio])) $_POST[radio]='f';
	if (!empty($_POST[address])) {
		$par[reseller_id]="$id";
		$par[ip_addr]="$_POST[address]";
		$par[demo_mode]="$_POST[radio]";
		//------------------------------------------------------
			$sql['type']='INSERT';
			$sql['params']=$par;
			$sql['table']='api_settings';
			$sql['where']="";
			$sql['options']='';
			$sql['return']='';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81)) {
			exit("<h4><font color=red>1 $database_error</font></h4>");
		}
                //------------------------------------------------------
                        $sql['type']='SELECT';
                        $sql['params']='*';
                        $sql['table']='api_settings';
                        $sql['where']="reseller_id='$id'";
                        $sql['options']='';
                        $sql['return']='struct';
                //------------------------------------------------------
                if (!$result=sql_exec($sql,$dbh81,'assoc')) {
                        exit("<h4><font color=red>2 $database_error</font></h4>");
                }
                $addr=$result[0][ip_addr];
                if (empty($addr)) {
                        $addr='none';
                } else {
                        $addr=preg_replace("/,/","\n",$addr);
                }

                $SETTINGS["baseTMName"]="ResselersPanel Team";
                $SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

                $mail_array= getMail ("api_ip_added", Array(
                                "fullname"=>$fullname,
                                "ip"=>$_POST[address],
                                "ip_list"=>$addr,
				"username"=>$nick_name,
                                "date"=>$now_date,
                                "mainserver" => "ResellersPanel"
                                ), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                $email[from]= $mail_array[from];
                $email[mime]= $mail_array[mime];
                $email[subject]= $mail_array[subject];
                $email[body]=$mail_array[body];
                if (!mail($email[address],$email[subject],$email[body],
                        "From: ".$email[from]."\r\n"
                        ."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
                                $mail=serialize($email);
                                mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
                }
		if ($_POST[radio]=='f') {
                        $demo='off';
                } elseif ($_POST[radio]=='t') {
                        $demo='on';
                }
                $mail_array= getMail ("api_demo_changed", Array(
                                "fullname"=>$fullname,
                                "demo"=>$demo,
				"username"=>$nick_name,
                                "date"=>$now_date,
                                "mainserver" => "ResellersPanel"
                                ), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                $email[from]= $mail_array[from];
                $email[mime]= $mail_array[mime];
                $email[subject]= $mail_array[subject];
                $email[body]=$mail_array[body];
                if (!mail($email[address],$email[subject],$email[body],
                        "From: ".$email[from]."\r\n"
                        ."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
                                $mail=serialize($email);
                                mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
                }
        } else {
		$par[reseller_id]="$id";
                $par[demo_mode]="$_POST[radio]";
                //------------------------------------------------------
                        $sql['type']='INSERT';
                        $sql['params']=$par;
                        $sql['table']='api_settings';
                        $sql['where']="";
                        $sql['options']='';
                        $sql['return']='';
                //------------------------------------------------------
                if (!$result=sql_exec($sql,$dbh81)) {
                        exit("<h4><font color=red>3 $database_error</font></h4>");
                }
		if ($_POST[radio]=='f') {
                        $demo='off';
                } elseif ($_POST[radio]=='t') {
                        $demo='on';
                }
                $SETTINGS["baseTMName"]="ResselersPanel Team";
                $SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

                $mail_array= getMail ("api_demo_changed", Array(
                                "fullname"=>$fullname,
                                "demo"=>$demo,
				"username"=>$nick_name,
                                "date"=>$now_date,
                                "mainserver" => "ResellersPanel"
                                ), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                $email[from]= $mail_array[from];
                $email[mime]= $mail_array[mime];
                $email[subject]= $mail_array[subject];
                $email[body]=$mail_array[body];
                if (!mail($email[address],$email[subject],$email[body],
                        "From: ".$email[from]."\r\n"
                        ."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
                                $mail=serialize($email);
                                mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
                }
	}
}
?>
